<?php

// +----------------------------------------------------------------------
// | Apprh.Shop
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.apprh.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// +----------------------------------------------------------------------
// | Author: 
// +----------------------------------------------------------------------

namespace app\admin\controller;

use controller\BasicAdmin;
use service\DataService;
use service\ToolsService;
use service\LogService;
use think\Db;

/**
 * 特色诊疗技术管理
 * Class Characteristic
 * @package app\admin\controller
 * @author  
 * @date 2019/07/25
 */
class Characteristic extends BasicAdmin
{
    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'Characteristic';
    
    public function _initialize()
    {
        parent::_initialize();
        // halt($this->diseases);
    }

    /**
     * Characteristic 列表
     * @return mixed
     */
    public function index()
    {
        $title = $this->request->param('title');
        $this->assign('title', $title);
        $this->title = '特色诊疗技术管理';
        $get = $this->request->get();
        // $map = ['is_deleted' => 0];
        if(isset($title)){
            $map['title'] = ['like', "%".$title."%"];
        }
        $db = Db::name($this->table)->where($map);

        // 应用搜索条件
        foreach (['title', 'aid', 'nid', 'did'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '') {
                if(in_array($key, ['aid', 'nid', 'did'])){
                    $db->where($key, 'eq', $get[$key]);
                    $db->whereOr($key, 'like', "{$get[$key]},%");
                    $db->whereOr($key, 'like', "%,{$get[$key]},%");
                    $db->whereOr($key, 'like', "%,{$get[$key]}");
                }else{
                    $db->where($key, 'like', "%{$get[$key]}%");
                }
            }
        }
        $data = parent::_list($db, true, false);
        return $this->fetch('', $data);
    }

    /**
     * 添加
     */
    public function add() {
        return $this->_form($this->table, 'form');
    }

    /**
     * 编辑
     */
    public function edit() {
        return $this->add();
    }

    /**
     * 删除
     */
    public function del() {
        if (DataService::update($this->table)) {
            LogService::write('系统设置', '特色诊疗删除成功');
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }

    /**
     * 禁用
     */
    public function forbid() {
        if (DataService::update($this->table)) {
            LogService::write('系统设置', '特色诊疗取消审核成功');
            $this->success("取消审核成功！", '');
        }
        $this->error("取消审核失败，请稍候再试！");
    }

    /**
     * 启用
     */
    public function resume() {
        if (DataService::update($this->table)) {
            LogService::write('系统设置', '特色诊疗通过审核成功');
            $this->success("通过审核成功！", '');
        }
        $this->error("通过审核失败，请稍候再试！");
    }

    //批量审核
    public function statusAll(){
        $map['id'] =array('in',input('post.ids/a'));
        Db::name($this->table)->where($map)->update(['status'=>1]);
        LogService::write('特色诊疗技术', '特色诊疗技术批量审核成功');
        $result['code'] = 1;
        $result['msg'] = '批量审核成功！';
        $result['url'] = url('index',array('catid'=>input('post.catid')));
        return $result;
    }

    //取消批量审核
    public function statusCancelAll(){
        $map['id'] =array('in',input('post.ids/a'));
        Db::name($this->table)->where($map)->update(['status'=>0]);
        LogService::write('特色诊疗技术', '特色诊疗技术批量取消审核成功');
        $result['code'] = 1;
        $result['msg'] = '批量取消审核成功！';
        $result['url'] = url('index',array('catid'=>input('post.catid')));
        return $result;
    }

    /**
     * 表单数据前缀方法
     * @param array $vo
     */
    public function _form_filter(&$data)
    {
        if ($this->request->isPost()) {
            // halt($data);
            if (isset($data['id'])) {
                LogService::write('系统管理', '特色诊疗修改成功');
                $data['updatetime'] = time();
            }else{
                LogService::write('系统管理', '特色诊疗添加成功');
                $data['userid'] = session('user.id');
                $data['aid'] = session('user.aid');
                $data['createtime'] = time();
            }
            if(!empty($data['readgroup'])){
                $data['readgroup']=trim(implode(',',$data['readgroup']),',');
            }
        }else{
            // halt($data);
            if(!empty($data['readgroup'])){
                $data['readgroup']=explode(',',$data['readgroup']);
            }else{
                $data['readgroup'] = [1];
            }
            if(!empty($data['aid'])){
                $data['aid']=explode(',',$data['aid']);
            }
            if(!empty($data['nid'])){
                $data['nid']=explode(',',$data['nid']);
            }
            if(!empty($data['did'])){
                $data['did']=explode(',',$data['did']);
            }
            // $newarea = $this->AR->where(['area_open' => 1, 'area_parent_id' => 0])->field('area_id as value, area_name as name')->select();
            $newareas = ToolsService::arr2subs($this->newareas, $data['aid']);
            $this->assign('newareas', $newareas);
            $newnations = ToolsService::arr2subss($this->newnations, $data['nid']);
            $this->assign('newnations', $newnations);
            $newdiseases = ToolsService::arr2subss($this->newdiseases, $data['did']);
            $this->assign('newdiseases', $newdiseases);
            // halt($newdiseases);
        }
        // halt(get_ext_title('1,2','nation', 'title'));
    }

}
